package com.itheima.service;

import com.itheima.mapper.ItemMapper;
import com.itheima.mapper.LoginDataMapper;
import com.itheima.pojo.Item;
import com.itheima.pojo.LoginData;
import com.itheima.util.SqlSessionFactoryUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.Date;

public class LoginDataService {
    SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();

    public Integer addLogin(LoginData loginData) {
        //调用LoginDataMapper.addLogin()

        //2. 获取SqlSession
        SqlSession sqlSession = factory.openSession();
        //3. 获取LoginDataMapper
        LoginDataMapper mapper = sqlSession.getMapper(LoginDataMapper.class);

        //4. 调用方法
        mapper.addLogin(loginData);
        Integer id = mapper.getLastInsertId(); // 获取最新插入的数据自动生成的id主键

        // 提交事务
        sqlSession.commit();
        // 释放资源
        sqlSession.close();

        return id;
    }

    public void updateLogoutTime(Date logoutTime, Integer id) {
        //调用LoginDataMapper.updateLogoutTime()

        //2. 获取SqlSession
        SqlSession sqlSession = factory.openSession();
        //3. 获取LoginDataMapper
        LoginDataMapper mapper = sqlSession.getMapper(LoginDataMapper.class);

        //4. 调用方法
        mapper.updateLogoutTime(logoutTime, id);

        // 提交事务
        sqlSession.commit();
        // 释放资源
        sqlSession.close();
    }
}
